const { RESOURCE_URL } = require('../config/database')
const { getLastStr } = require('../utils/tool')

// 访问资源
const accessResource = (req, res) => {
	const url = req.url.replace('/resource', '')
	// /Users/wangdaibin/Desktop/resource/excel
	res.sendFile(`${RESOURCE_URL}${url}`);//图片路径
	// res.sendFile(__dirname + `${req.url}`);//绝对路径
	// res.sendFile(`${req.url}`, { root: __dirname });//根路径
}

// 下载Excel
const exportExcel = (req, res) => {
	// 设置文件名
	// res.setHeader('Content-disposition', 'attachment; filename=example.xlsx');
	// res.setHeader('Content-type', 'application/octet-stream;charset=UTF8');
	const filePath = `${RESOURCE_URL}/excel/人员信息.xlsx`; // Excel文件路径

	res.download(filePath, 'personInfo.xlsx', (err) => {
		if (err) {
			console.error(err);
			res.status(500).send('Error downloading file');
		}
	});
}
module.exports = {
	accessResource,
	exportExcel
}